using System;
using System.Drawing;

namespace org.paulbanks.BarcodeDotNet
{
	/// <summary>
	/// Provides drawing functions for the specified encoding object
	/// </summary>	
	public class DrawBarcode {

		private Brush BarBrush;
		private Brush SpaceBrush;

		public DrawBarcode() {						
			BarBrush = Brushes.Black;
			SpaceBrush = Brushes.White;
		}

		public DrawBarcode(Color clrBar, Color clrSpace) {
			BarBrush = new SolidBrush(clrBar);
			SpaceBrush = new SolidBrush(clrSpace);
		}

		public int Draw(Graphics g, int py, int px, int nHeight, int wfactor, string strText, BarcodeEncoder encoder)  {
		
			string strEnc = encoder.Encode(strText);
			char[] c = strEnc.ToCharArray();
			
			int xPos = 0;
			Brush b;	// Bar or space
			int w;		// Width of bar/space
			for (int i=0;i<strEnc.Length;i++) {

				// Bar or space
				b = SpaceBrush;
				if (i%2!=0) b = BarBrush; 
														
				// Width
				if (Char.IsDigit(c[i]))
					w = c[i]-'0';
				else
					w = c[i]-'a'+1;
                w *= wfactor;              			 
				
				if (g!=null) g.FillRectangle(b, px+xPos, py, w, nHeight);

				xPos += w;

			}     
            
            return xPos;       

		}

		public int GetWidth(int wfactor, string strText, BarcodeEncoder encoder)  {
			return Draw(null, 0, 0, 1, wfactor, strText, encoder);
		}

	}
}
